message(NOTICE "开始构建 fdk-aac 模块")
# 检查构建目标是否存在
if(TARGET fdk-aac-${SPEEX_TAG})
    message(NOTICE "fdk-aac 模块已存在 fdk-aac-${FDK_AAC_TAG}")
 return()
endif()
# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/fdk-aac/${FDK_AAC_TAG}
    ${MODULE_INSTALL_DIR}/fdk-aac/${FDK_AAC_TAG}
    fdk-aac-${FDK_AAC_TAG})
# 配置库参数成成规则
set(FDK_AAC_CONFIGURE CACHE STRING "fdk-aac configure" FORCE)
if(BUILD_SHARED_LIBS)
    # 开启 编译动态库 ON
    list(APPEND FDK_AAC_CONFIGURE "-DBUILD_SHARED_LIBS=ON")
endif()
if(BUILD_STATIC_LIBS)
    # 开启 编译静态库 ON
    list(APPEND FDK_AAC_CONFIGURE "-DBUILD_SHARED_LIBS=OFF")
endif()
# Android 配置 模块
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
    # 编译器工具链
    list(APPEND FDK_AAC_CONFIGURE "-DANDROID_ABI=${ANDROID_ABI}")
    list(APPEND FDK_AAC_CONFIGURE "-DANDROID_PLATFORM=${ANDROID_PLATFORM}")
    list(APPEND FDK_AAC_CONFIGURE "-DANDROID_STL=${ANDROID_STL}")
    list(APPEND FDK_AAC_CONFIGURE "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
    list(APPEND FDK_AAC_CONFIGURE "-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}")
    list(APPEND FDK_AAC_CONFIGURE "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}")
    list(APPEND FDK_AAC_CONFIGURE "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
    list(APPEND FDK_AAC_CONFIGURE "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
endif()

# 构建 fdk-aac 库
ExternalProject_Add(
    fdk-aac-${FDK_AAC_TAG}
    # 目录配置
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                      # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/fdk-aac/${FDK_AAC_TAG}          # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/fdk-aac/${FDK_AAC_TAG}        # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/fdk-aac/${FDK_AAC_TAG}  # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/fdk-aac/${FDK_AAC_TAG}        # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/fdk-aac/${FDK_AAC_TAG}          # ✅ 显式设置 tmp 目录

    DOWNLOAD_COMMAND # 下载命令
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${FDK_AAC_REPO}
            -DGIT_TAG=${FDK_AAC_TAG}
            -P ${__DIR}/utils/git.cmake
   # 修复 Android 编译问题
# 修复源码（使用 CMake 脚本，安全可靠）
    COMMAND ${CMAKE_COMMAND}
        -DSOURCE_DIR=${MODULE_SRC}/fdk-aac/${FDK_AAC_TAG}
        -P ${__DIR}/utils/fix-fdk-aac.cmake
    CONFIGURE_COMMAND
        COMMAND ${CMAKE_COMMAND}
            -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
            -DBUILD_PROGRAMS=OFF # 不编译执行程序，可以保持 OFF。
            -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
            -DFDK_AAC_INSTALL_CMAKE_CONFIG_MODULE=ON	# CMake 配置文件
            -DFDK_AAC_INSTALL_PKGCONFIG_MODULE=ON	# 安装 fdk-aac.pc
            -S <SOURCE_DIR> # 源码目录
            -B <BINARY_DIR> # 构建目录
            ${FDK_AAC_CONFIGURE}

    BUILD_COMMAND
        ${CMAKE_COMMAND} --build <BINARY_DIR> --config ${CMAKE_BUILD_TYPE} -j${NPROC}
    INSTALL_COMMAND
        ${CMAKE_COMMAND} --install <BINARY_DIR> --config ${CMAKE_BUILD_TYPE}
            # 将安装文件路径保存到 文件去
            COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     ON                            # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志
   # 依赖目标 安装需要先安装 automake libtool autopoint
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)
# 导入 speex 库
add_library(fdk-aac::fdk-aac STATIC IMPORTED)
# 属性配置
set_target_properties(fdk-aac::fdk-aac PROPERTIES
    IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
    IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/fdk-aac/${FDK_AAC_TAG}/lib/libfdk-aac.a
    INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/fdk-aac/${FDK_AAC_TAG}/include
)
# 依赖目标
add_dependencies(fdk-aac::fdk-aac fdk-aac-${FDK_AAC_TAG})



